var vm = new Vue({
    el: '#contentContainer',
    data: {
        form: {
            orgId: '',
            orgName: '',
            orgCode: '',
            parentIdList: null,
            priority: 0
        },
        rules: {
            orgName: [
                {required: true, message: '请输入机构名', trigger: 'blur'},
                {min: 0, max: 100, message: '长度不能超过100个字符', trigger: 'blur'}
            ],
            orgCode: [
                {required: true, message: '请输入机构编码', trigger: 'blur'},
                {min: 0, max: 100, message: '长度不能超过100个字符', trigger: 'blur'}
            ],
            parentIdList: [
                {type: 'array', required: true, message: '请选择父机构', trigger: 'change'}
            ]
        },
        options: [{id: 0, text: '根节点'}],
        props: {
            children: 'children',
            label: 'text',
            value: 'id'
        }
    },
    created: function () {
        var $this = this;

        Common.getJson('/sys/org/getOrgInfo',
            {orgId: Common.getUrlParam('orgId')},
            function (response) {
                if (!response || response.status != 200)
                    return;

                var orgInfo = response.result;
                $this.form.orgId = orgInfo.orgId;
                $this.form.orgName = orgInfo.orgName;
                $this.form.orgCode = orgInfo.orgCode;
                $this.form.priority = orgInfo.priority;
                $this.form.parentIdList = [];

                Common.getJson('/sys/org/getOrgTreeList', {},
                    function (response) {
                        if (!response || response.status != 200)
                            return;

                        var orgTreeList = response.result;

                        // 属性配置信息
                        var attributes = {
                            // 标识字段名
                            keyField: 'orgId',
                            // 上级标识字段名
                            parentKeyField: 'parentId',
                            // 文本字段名
                            textField: 'orgName',
                            // 根节点标识
                            rootParentId: 0
                        };

                        //编辑状态下，当前节点及其子节点都不能被选择
                        //需要从列表中剔除当前节点及其子节点
                        for (var i = 0; i < orgTreeList.length; i++) {
                            if (orgTreeList[i].orgId == orgInfo.orgId) {
                                delete orgTreeList[i];
                                break;
                            }
                        }

                        $this.options[0].children = Common.convertTreeData(orgTreeList, attributes);

                        //获取当前节点的父节点列表
                        if (orgInfo.parentId != 0) {
                            var parentId = orgInfo.parentId;
                            $this.form.parentIdList = Common.getTreeParentIdList(orgTreeList, parentId, 0, attributes);
                        }

                        $this.form.parentIdList.unshift(0);
                    });
            });
    },
    methods: {
        submitForm: function (formName) {
            var $this = this;

            $this.$refs[formName].validate(function (valid) {
                if (!valid)
                    return false;

                Common.postJson('/sys/org/edit', $this.form,
                    function (response) {
                        if (!response)
                            return;

                        //保存成功
                        if (response.status == 200 && response.result.success)
                            window.location.href = '/sys/org/list';
                        else {
                            var errMsg = response.status == 200 ? response.result.errMsg : response.msg;

                            $this.$notify.error({
                                title: '错误',
                                message: errMsg,
                                offset: $("#main", window.parent.document).height() - 120,
                                duration: 0
                            });
                        }
                    });
            });
        },
        resetForm: function (formName) {
            this.$refs[formName].resetFields();
        }
    }
});